/*******************************************************************************
**
** Copyright (c) 2011 - The OWE Team
**
** This file is part of OWE.
**
** OWE is a free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; version
** version 2.1 of the License.
**
** OWE is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with OWE. If not, see <http://www.gnu.org/licenses/>.
**
*******************************************************************************/

#include <QDomNode>

#include "xmlnode.h"

OWE_USE_NAMESPACE

XMLNode::XMLNode(const QDomNode &node)
    : node(node)
{
}

UString XMLNode::name() const
{
    if (node.nodeName() == "#text")
        return UString("text");

    return UString(node.nodeName().toUtf8().data());
}

UString XMLNode::attribute(const char *attributeName) const
{
    QDomElement element = node.toElement();

    if (element.isNull())
        return UString();

    QString attribute;
    if (UString(attributeName) == "xml:lang")
        attribute = findLang();
    else
        attribute = element.attribute(attributeName);

    return UString(attribute.toUtf8().data());
}

UString XMLNode::data() const
{
    return UString(node.nodeValue().toUtf8().data());
}

QString XMLNode::findLang() const
{
    QDomElement element = node.toElement();

    while (!element.isNull() && !element.hasAttribute("xml:lang"))
        element = element.parentNode().toElement();

    return element.attribute("xml:lang");
}
